/****************************************************
 * Description: Entity for 角色
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
    *  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuol.bfsuolframework.security.dao;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.bfsuol.bfsuolframework.core.entity.Entity;
import com.bfsuol.bfsuolframework.core.entity.EntitySupport;
import lombok.Data;
import lombok.experimental.Accessors;
import org.codehaus.jackson.annotate.JsonIgnore;

import java.util.Collection;
import java.util.HashSet;

@TableName("t_sec_role")
@Data
@Accessors(chain = true)
public class Role extends EntitySupport implements Entity {

    private static final long serialVersionUID = 1L;

    public Role(){}
    public Role(Long id){
        this.id = id;
    }

    @TableId(type = IdType.AUTO,value = "role_id")
    private Long id;
    @TableField(value = "title")
    private String title;//名称
    @TableField(value = "code")
    private String code;//代码
    @TableField(value = "description")
    private String description;//描述
    @TableField(value = "is_enabled")
    private Integer isEnabled;//是否可用
    @TableField(value = "is_sys")
    private Integer isSys;//是否系统角色

    @TableField(exist = false)
    private transient Collection<BaseUser> users;//用户
    @TableField(exist = false)
    private transient Collection<RolePrivilege> privileges;//权限

    public boolean isNew() {
        return getId() == null || getId().longValue()<=0;
    }

    /**
     */
    public void addUser(BaseUser user) {
        if(this.users == null){
            this.users = new HashSet<BaseUser>();
        }
        this.users.add(user);
    }

    /**
     */
    public void removeUser(BaseUser user) {
        if(this.users == null){
            return;
        }
        this.users.remove(user);
    }

    /**
     */
    public void addPrivilege(RolePrivilege rolePrivilege) {
        if(this.privileges == null){
            this.privileges = new HashSet<RolePrivilege>();
        }
        this.privileges.add(rolePrivilege);
    }

    /**
     */
    public void removePrivilege(RolePrivilege rolePrivilege) {
        if(this.privileges == null){
            return;
        }
        this.privileges.remove(rolePrivilege);
    }
}

